<?php

class Salonfecha extends MY_Model {

 
    function Salonfecha()
    {
        // Call the Model constructor
        parent::MY_Model();
	
	$this->nombreTabla = "salonfecha";
	$this->idT1= "idSalonFecha";
	$this->nombreCampo="fechaDesde";


		$this->campos[] = array("realname" => "idSalon",
					"clase" => "partof",
					"label" => "Salon",
					"modelo" => "salon",
					"rules" => "required|integer");
		
		$this->campos[] = array("realname" => "fechaDesde",
					"clase" => "date",
					"label" => "Fecha Desde",
					"rules" => "required|min_length[3]|max_length[30]");

		$this->campos[] = array("realname" => "fechaHasta",
					"clase" => "date",
					"label" => "Fecha Hasta",
					"rules" => "required|min_length[3]|max_length[30]");

		$this->campos[] = array("realname" => "lunes",
					"clase" => "boolean",
					"label" => "Lunes",
					"rules" => "");

		$this->campos[] = array("realname" => "martes",
					"clase" => "boolean",
					"label" => "Martes",
					"rules" => "");

		$this->campos[] = array("realname" => "miercoles",
					"clase" => "boolean",
					"label" => "Miercoles",
					"rules" => "");

		$this->campos[] = array("realname" => "jueves",
					"clase" => "boolean",
					"label" => "Jueves",
					"rules" => "");

		$this->campos[] = array("realname" => "viernes",
					"clase" => "boolean",
					"label" => "Viernes",
					"rules" => "");

		$this->campos[] = array("realname" => "sabado",
					"clase" => "boolean",
					"label" => "Sabado",
					"rules" => "");

		$this->campos[] = array("realname" => "domingo",
					"clase" => "boolean",
					"label" => "Domingo",
					"rules" => "");
		

    }
     

	function superposicion($data){

		$query=$this->db->query("select * from salonfecha where ('".$data['fechaDesde']."' between fechaDesde and fechaHasta or (fechaDesde between '".$data['fechaDesde']."' and '".$data['fechaHasta']."') or (fechaHasta  between '".$data['fechaDesde']."' and '".$data['fechaHasta']."') or ( '".$data['fechaHasta']."' between fechaDesde and fechaHasta)) and (".$data['lunes']."*lunes=1 or ".$data['martes']."*martes=1 or ".$data['miercoles']."*miercoles=1 or ".$data['jueves']."*jueves=1 or ".$data['viernes']."*viernes=1 or ".$data['sabado']."*sabado=1 or ".$data['domingo']."*domingo=1) and (idSalon in (select idSalon from salon where idResto=(select idResto from salon where idSalon=".$data['idSalon'].")))");
		if ($query->num_rows() > 0)
			return true;
		return false;

/*select * from salonfecha where ('2009-11-11' between fechaDesde and fechaHasta 
or (fechaDesde between '2009-11-11' and '2010-11-11')
or (fechaHasta  between '2009-11-11' and '2010-11-11')
or ( '2010-11-11' between fechaDesde and fechaHasta))
and (1*lunes=1 or 0*martes=1 or 0*miercoles=1 or 0*jueves=1 or 0*viernes=1 or 0*sabado=1 or 0*domingo=1)
and (idSalon in (select idSalon from salon where idResto=(select idResto from salon where idSalon=1)))*/
	}


     protected function insertar($foreign,$file='')
	{

		$data=array();
		
		foreach($this->campos as $campo){

			switch ($campo['clase']){
			case "foranea":
				$data[$campo['realname']] = $foreign[$campo['modelo']];
				break;
			case "file":
				$data[$campo['realname']] = $file;
				break;
			case "boolean":
				if ($this->input->post($campo['realname'])=='true')			
					$data[$campo['realname']] = '1';
				else
					$data[$campo['realname']] = '0';
				break;
			case "datetime":
				break;
			default:
				$data[$campo['realname']] = $this->input->post($campo['realname']);
			}
		}
		if ($this->superposicion($data)){
			$this->sesion->set_userdata('error',"<p>No se pudo argregar el nuevo rango de fechas, entra en conflicto con otro salon</p>");
			return 0;
		}else{
			$this->db->insert($this->nombreTabla, $data);
			return $this->db->insert_id();
		}
		return 0;
		
		
	}

	
     protected function modificar($file='')
	{
		$aux=array();		

		$data=array();
		foreach($this->campos as $campo){
			switch ($campo['clase']){
				case "file":
					if ($file!='') $data[$campo['realname']] = $file;
					break;
				case "foranea": 
					$aux[]=$campo['modelo'];
					break;
				case "boolean":
					if ($this->input->post($campo['realname'])=='true')			
						$data[$campo['realname']] = '1';
					else
						$data[$campo['realname']] = '0';
					break;
				default:
					$data[$campo['realname']] = $this->input->post($campo['realname']);

			}
		}
		$this->db->where($this->idT1,  $this->input->post($this->idT1) );
		$this->db->update($this->nombreTabla, $data);
		
		return $aux;
	}
 
}


?>
